package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityDishesFoodstuff;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.food.FoodstuffIdRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDishesFoodstuff;
import com.ocom.payconfig.interceptor.ScopeQueryWrapper;
import com.ocom.payconfig.service.DishesFoodstuffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishesFoodstuffServiceImpl extends ServiceImpl<IDaoDishesFoodstuff, EntityDishesFoodstuff> implements DishesFoodstuffService {

    @Autowired
    IDaoDishesFoodstuff iDaoDishesFoodstuff;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer getCountByFoodstuffId(Long companyId, FoodstuffIdRequest request) {
        ScopeQueryWrapper<EntityDishesFoodstuff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("foodstuff_id", request.getFoodstuffId());
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean deleteByFoodstuffId(Long companyId, FoodstuffIdRequest request) {
        ScopeQueryWrapper<EntityDishesFoodstuff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("foodstuff_id", request.getFoodstuffId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDishesFoodstuff> getList(Long companyId, Integer merchId, Integer dishesId) {
        return iDaoDishesFoodstuff.getList(companyId, merchId, dishesId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateOne(Long companyId, EntityDishesFoodstuff request) {
        ScopeQueryWrapper<EntityDishesFoodstuff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("dishes_id", request.getDishesId());
        queryWrapper.eq("foodstuff_id", request.getFoodstuffId());
        queryWrapper.eq("price_mode", request.getPriceMode());
        queryWrapper.last("limit 1");
        EntityDishesFoodstuff entityDishesFoodstuff = this.getOne(queryWrapper);
        if (entityDishesFoodstuff != null) {
            entityDishesFoodstuff.setWeight(request.getWeight());
            return this.save(entityDishesFoodstuff);
        } else {
            return this.save(request);
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean deleteOne(Long companyId, EntityDishesFoodstuff request) {
        ScopeQueryWrapper<EntityDishesFoodstuff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("dishes_id", request.getDishesId());
        queryWrapper.eq("foodstuff_id", request.getFoodstuffId());
        queryWrapper.eq("price_mode", request.getPriceMode());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<EntityDishesFoodstuff> getList(Long companyId, List<Integer> dishesIds) {
        ScopeQueryWrapper<EntityDishesFoodstuff> queryWrapper = new ScopeQueryWrapper<>(1);
        queryWrapper.eq("com_id", companyId);
        queryWrapper.in("dishes_id", dishesIds);
        queryWrapper.eq("price_mode", 20);
        return this.list(queryWrapper);
    }

}
